<template>
  <!-- 岗位管理 -->
  <div class="app-container">
    <div class="form-container">
      <el-form ref="queryForm" :model="filters" :inline="true" label-width="80px">
        <el-form-item label="岗位名称" prop="postName">
          <el-input
            v-model="filters.postName"
            placeholder="请输入岗位名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input
            v-model="filters.postCode"
            placeholder="请输入岗位编码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-select v-model="filters.status" placeholder="岗位状态" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          />
        </el-form-item>
        <!-- 高级设置一栏 start -->
        <!-- <el-form-item>
          <slot name="label">
            <i class="highLevelStyle" :class="highLevelSet===true?'el-icon-arrow-up':'el-icon-arrow-down'" @click="highLevelSet = !highLevelSet" />
          </slot>
        </el-form-item> -->
        <!-- <high-level-setting v-if="highLevelSet" :parent-to-child="highLevelSettingData" @sendValueToParent="getValueFromChild" /> -->
      </el-form>
    </div>
    <div class="btns-group">
      <systemBtns :single="single" has-permi="post" />
      <el-button v-show="isStatusShow === '1'" v-hasPermi="[`system:post:edit`]" size="middle" :disabled="single" icon="el-icon-star-on" @click="handleStatusChange">
        启用
      </el-button>
      <el-button v-show="isStatusShow === '0'" v-hasPermi="[`system:post:edit`]" size="middle" :disabled="single" icon="el-icon-star-off" @click="handleStatusChange">
        停用
      </el-button>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      stripe
      class="table-list"
      :data="tableData"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column prop="postName" label="岗位名称" min-width="180" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="postCode" label="岗位编码" min-width="150" align="center" />
      <el-table-column prop="postSort" label="岗位顺序" min-width="100" align="center" />
      <el-table-column prop="status" label="状态" align="center" min-width="40" :formatter="statusFormat" />
    </el-table>
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改岗位对话框 -->
    <addDialog ref="addDialog" />
  </div>
</template>

<script>
import addDialog from './components/addDialog'
import { listPost, delPost, updatePost } from '@/api/system/post'
// 引入高级设置一栏
// import HighLevelSetting from '@/components/HighLevelSetting'
import { mapState } from 'vuex'
// 按钮组件
import systemBtns from '@/components/Common/Btns/systemBtns'
export default {
  name: 'Post',
  components: {
    // HighLevelSetting,
    systemBtns,
    addDialog
  },
  data() {
    return {
      // 高级设置显示隐藏
      highLevelSet: false,
      // 高级设置参数
      highLevelSettingData: [
        {
          label: '创建时间',
          isDatePicker: 1,
          size: 'small',
          type: 'daterange',
          separator: '-',
          start: '开始日期',
          end: '结束日期',
          isClearable: false,
          prop: 'createTime',
          model: []
        }
      ],
      // 动态保存控制
      isSubmit: false,
      // 时间范围数组
      dateRange: [],
      isStatusShow: '0',
      // 高亮数组
      selectRow: [],
      // 遮罩层
      loading: true,
      // 选中的行对象
      multipleSelection: [],
      // 非单个禁用
      single: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      tableData: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      filters: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        postName: '',
        postCode: '',
        status: '0',
        // 日期范围
        dateRange: ''
      }

    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_normal_disable').then(res => {
      this.statusOptions = res.data.rows
    })
  },
  methods: {
    // 获取高级设置中子组件传递的数据
    getValueFromChild(data) {
      this.highLevelQueryParams = data
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listPost(this.changeDateRange(this.filters, this.dateRange)).then(res => {
        this.loading = false
        if (res) {
          this.tableData = res.data.rows
          this.total = res.data.total
          // 遍历表格数据
          // this.addTaleData()
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // list表格填充空白行
    addTaleData() {
      const arr = {}
      if (this.tableData.length < this.filters.pageSize) {
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        selection = []
      }
      this.multipleSelection = selection
      if (selection.length === 1) {
        this.isStatusShow = this.multipleSelection[0].status
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      if (selection.length > 0) {
        selection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (selection.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(selection.pop())
      }
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },

    /** 搜索按钮操作 */
    handleQuery() {
      /* if (this.highLevelSet === true) {
        if (this.highLevelQueryParams[0].model !== '') {
          this.dateRange = this.highLevelQueryParams[0].model
        }
      } else {
        this.dateRange = []
      }
      this.filters.pageNum = 1*/
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.highLevelSettingData[0].model = []
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      const maxData = []
      let postSort = 1
      for (const i in this.tableData) {
        if (this.tableData[i].postSort !== undefined) {
          maxData.push(this.tableData[i].postSort)
        }
      }
      if (maxData.length !== 0) {
        postSort = Math.max.apply(null, maxData) + 1
      }
      this.$refs.addDialog.handleAdd('新增岗位', undefined, postSort)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = this.multipleSelection[0].postId
      this.$refs.addDialog.handleAdd('修改岗位', postId)
    },

    /** 删除按钮操作 */
    handleDelete() {
      let postIds = ''
      const child = this.multipleSelection
      for (let i = 0; i < child.length; i++) {
        postIds += child[i].postId + ','
      }
      // 去掉最后一个逗号
      if (postIds.length > 0) {
        postIds = postIds.substr(0, postIds.length - 1)
      }
      this.$confirm(
        '是否确认删除数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delPost(postIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function() {})
    },
    // 用户状态修改
    handleStatusChange() {
      this.resetQuery()
      const row = this.multipleSelection[0]
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '' + row.postName + '用户吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          if (row.status === '0') {
            row.status = '1'
          } else {
            row.status = '0'
          }
          const { postId, postCode, postName, postSort, remark, status } = row
          updatePost({ postId, postCode, postName, postSort, remark, status }).then(resp => {
            if (resp) {
              this.getList()
              this.msgSuccess('操作成功！')
            }
          })
        })
        .catch(() => {
        })
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    }
  }
}
</script>

<style scoped>
/*.form-container {*/
/*  position: relative;*/
/*  padding: 0 !important;*/
/*}*/
</style>
